package com.wise.service.cms.content.article;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.wise.entities.content.Article;
import com.wise.framework.basic.DateTimeUtil;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoSave;
import com.wise.framework.exception.LogicException;
import com.wise.framework.session.CurrentUserId;
import com.wise.model.content.enums.ArticleEnum;
import com.wise.summer.v1.cms.content.article.UpdateArticleById;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.sql.Timestamp;
import java.util.Objects;

import static com.wise.model.content.ContentErrMsgs.Err_001600250007;
import static com.wise.model.content.ContentErrMsgs.Err_001600250013;
import static com.wise.model.content.ContentErrMsgs.Err_001600250019;
import static com.wise.model.content.ContentErrMsgs.Err_001600250020;

@Service
@SummerService
public class UpdateArticleService implements SummerServiceBean<UpdateArticleById> {
    @Override
    public void sum(UpdateArticleById model) {
        Integer type = model.getType();
        ArticleEnum articleEnum = ArticleEnum.getThisFromType(type);
        if (articleEnum == null) {
            throw new LogicException(this.getClass(), Err_001600250020);
        }
        if (articleEnum.isHasColumn() && model.getColumnId() == null) {
            throw new LogicException(this.getClass(), Err_001600250013);
        }
        if (!articleEnum.isHasColumn()) {
            model.setColumnId(null);
        }
        //新闻标题不可重复
        AddArticleService.checkIfDuplicateTitle(model.getTitle(), model.getId(), model.getType());
        //查询文章
        Article article = DaoOne4Update.s(Article.class, model.getId(), Err_001600250007);

        // 别名不可重复
        model.setAlias(ArticleTools.toNullAlias(model.getAlias()));
        if (model.getAlias() != null && !model.getAlias().equals(article.getAlias())) {
            if (ArticleTools.checkAlias(model.getAlias(), model.getType())){
                throw new LogicException(this.getClass(), Err_001600250019);
            }
        }

        if (model.getDraftState() == null){
            model.setDraftState(false);
        }
        article.setPageTitle(model.getPageTitle());
        // 草稿
        if (model.getDraftState()){
            Article temp = new Article();
            BeanUtils.copyProperties(model, temp);
            String draftJson = JSON.toJSONString(temp);
            article.setDraft(draftJson);
            DaoSave.s(article);
        }else {
            BeanUtil.copyProperties(model, article);
            Timestamp publishTime = AddArticleService.getPublishTimeByState(model.getState(), model.getPublishTime());
            if (!Objects.isNull(publishTime)) {
                article.setPublishTime(publishTime);
            }
            article.setLastUpdateBy(CurrentUserId.s(true));
            article.setLastUpdateDate(DateTimeUtil.timestampNow());
            article.setDraft(null);
            DaoSave.s(article);
        }
        model.setSummerResult(article);
    }
}
